<?php 
include('../../Connections/KRAPPERCON.php');
?>

 
<?php 
/*
		code : GEBI('t_cod_lar').value,
		name : GEBI('t_nom_lar').value,
		marca : GEBI('cbo_mar_lar').value,
		clase : GEBI('cbo_cla_lar').value,
		family : GEBI('cbo_fam_lar').value,
		ubic : GEBI('t_ubi_lar').value,
		centro : GEBI('cbo_loc_lar').value,
		almac : GEBI('cbo_alm_lar').value
		stock : (GEBI('chk_stk').checked?1:0);
		*/
		
$cod = getVal('code');
$nam = getVal('name');
$marca = getVal('marca');
$clase = getVal('clase');
$fam = getVal('family');
$ubi = getVal('ubic');
$centro = getVal('centro');
$almc = getVal('almac');
$stock = getVal('stock');
$opc = getVal('opcion');


$s = '=';
if ( strpos($cod,'*') > 0) {
$s = 'like';
$cod = str_replace('*','%',$cod);
}

$s_cod = isNull($cod," and a.ArticuloID ".$s." '".$cod."'");
//$s_nam = $s_cod.isNull($nam, " and ArticuloName = '".$nam."'");
$s_nam = '';
$s_mar = isNull($marca, " and MarcaID = '".$marca."'");
$s_cla = isNull($clase, " and ClaseID = '".$clase."'");
$s_fam = isNull($fam," and FamilyID = '".$fam."'");
$s_ubi = isNull($ubi," and ArticuloUbi = '".$ubi."'");
$s_cen = isNull($centro," and CentroID = '".$centro."'");
$s_almc = isNull($almc," and AlmacenID = '".$almc."'");
$s_stk = isZeros($stock," and StockTot > 0");
$sql = '';		

$where2 = $s_cod.$s_nam.$s_mar.$s_cla.$s_fam;

$where = $s_cod.$s_nam.$s_mar.$s_cla.$s_fam.$s_almc.$s_cen.$s_ubi.$s_stk;

if (trim($where)!=''){
	$where = "where ".substr($where,4);
}
if (trim($where2)!=''){
	$where2 = "where ".substr($where2,4);
}


if ($opc == 1) {
?>
<table width="100%" border="1"  rules="all" class="tableIn tblSort" id="tbl_qart" cellspacing="0" cellpadding="0">
<thead>
  <tr>
    <th>Articulo</th>
    <th>Descripcion</th>
    <th>Clase</th>
    <th>Centro</th>
    <th>Almacen</th>
    <th>Ubicacion</th>
    <th>Stk.Tot.</th>
    <th>Stk.Dis.</th>
    <th>Costo</th>
    <th>Valor.Total</th>
    <th>Precio</th>
    <th>Dscto1</th>
    <th>Dscto2</th>
    <th>Reemplazo</th>
  </tr>
  </thead>
  <tbody>
<?php 

$sql = 'select a.ArticuloID,b.CentroID,a.ArticuloName,a.ClaseID,a.UserID ,
a.ArticuloIDNew, a.ArticuloFeCre,b.ArtAlmCos ArticuloCos,a.ArticuloPre,
b.ArticuloUbi, b.ArtAlmDsc1 ArticuloDsc1, b.ArtAlmDsc2 ArticuloDsc2, b.AlmacenID,StockTot,StockLib from tt_articulo a left join tt_articuloalm b
on a.ArticuloID = b.articuloID '.$where;
doEcho ($sql);
$sm_stot = 0;
$sm_sdis = 0;
$sm_vcos = 0;
$res = $mysqli->query($sql);
if ($res){
	while($row = $res->fetch_assoc()){
	$sm_stot += $row['StockTot'];
	$sm_sdis += $row['StockLib'];
	$val_costo = $row['StockTot'] * $row['ArticuloCos'];
	$sm_vcos += $val_costo;
	?>
    <tr>
    <td><a href="#" class="a"><?php echo $row['ArticuloID']; ?></a></td>
    <td><?php echo $row['ArticuloName']; ?></td>
    <td><?php echo $row['ClaseID']; ?></td>
    <td><?php echo $row['CentroID']; ?></td>
    <td><?php echo $row['AlmacenID']; ?></td>
    <td><?php echo $row['ArticuloUbi']; ?></td>
    <td class="qt"><?php echo $row['StockTot']; ?></td>
    <td class="qt"><?php echo $row['StockLib']; ?></td>
    <td class="qt"><?php echo $row['ArticuloCos']; ?></td>
    <td class="qt"><?php echo myRound($val_costo);?></td>
    <td class="qt"><?php echo $row['ArticuloPre']; ?></td>
    <td class="qt"><?php echo $row['ArticuloDsc1']; ?></td>
    <td class="qt"><?php echo $row['ArticuloDsc2']; ?></td>

    <td><?php echo $row['ArticuloIDNew']; ?></td>
 
  <?php 
	}

$res->close();

$mysqli->check_result();

}
?>
</tbody>
<tfoot>
 </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td class="qt"><?php echo myRound($sm_stot);?></td>
      <td class="qt"><?php echo myRound($sm_sdis);?></td>
      <td class="qt"><?php ?></td></td>
      <td class="qt"><?php echo myRound($sm_vcos);?></td></td>
      <td class="qt">&nbsp;</td>
      <td class="qt">&nbsp;</td>
      <td class="qt">&nbsp;</td>
      <td>&nbsp;</td>
    </tr>	
</tfoot>
</table>
<?php 
}
else {
?>
<table width="100%" border="1"  rules="all" class="tableIn" id="tbl_qart" cellspacing="0" cellpadding="0">
<thead>
  <tr>
    <th>Articulo</th>
    <th>Descripcion</th>
    <th>Clase</th>
    <th>Precio</th>
    <th>Dscto1</th>
    <th>Dscto2</th>
    <th>Reemplazo</th>
  </tr>
  </thead>
  <tbody>
  <?php 

$sql = 'select a.ArticuloID,a.ArticuloName,a.ClaseID,a.UserID ,
a.ArticuloIDNew, a.ArticuloFeCre,a.ArticuloCos,a.ArticuloPre,ArticuloDsc1, ArticuloDsc2  from tt_articulo_all a '.$where2;
doEcho( $sql);
$res = $mysqli->query($sql);
if ($res){
	while($row = $res->fetch_assoc()){
	?>
    <tr>
    <td><a class="a" href="#" ><?php echo $row['ArticuloID']; ?></a></td>
    <td><?php echo $row['ArticuloName']; ?></td>
    <td><?php echo $row['ClaseID']; ?></td>
    <td><?php echo $row['ArticuloPre']; ?></td>
    <td><?php echo $row['ArticuloDsc1']; ?></td>
    <td><?php echo $row['ArticuloDsc2']; ?></td>

    <td><?php echo $row['ArticuloIDNew']; ?></td>
  </tr>	
  <?php 
	}

$res->close();
$mysqli->check_result();
}
?>
  </tbody>
  </table>
<?php 
}
?>
<script type="text/javascript">
$("#tbl_qart tbody .a").each(function(){
	this.href='#page/Mantenimientos/MantArt.php?oper=view&tip=ac&codart='+this.innerHTML;
});
</script>
